<?php
class Kernel {

    private $plugin = null;
    public $v = null;
    public $model = null;
    public $setting = null;

    public function __construct() {
        $this->v = C( 'Template' );
        $this->model = C( 'Model' );
        $this->setting = $GLOBALS[ 'setting' ];
        $this->init_plugin();
    }

    // 初始化插件
    private function init_plugin() {
        $plugin = $this->model->Cache()->get( 'plugin' );
        foreach ( $plugin as $k => $v ) {
            $p_file = APP_PLUGIN . "{$v['pid']}/{$v['pid']}.php";
            if ( !$v[ 'status' ] || !is_readable( $p_file ) ) {
                unset( $plugin[ $k ] );
                continue;
            }
            require_once $p_file;
        }
        $this->plugin = $plugin;
    }

    // 运行插件
    public function doAction( $model ) {
        $args = array_slice( func_get_args(), 1 );

        foreach ( $this->plugin as $v ) {
            $obj = C( $v[ 'pid' ] );
            if ( method_exists( $obj, 'input' ) ) {
                $obj->input( $this );
            }
            if ( method_exists( $obj, $model ) ) {
                if ( $reval = $obj->$model( $args ) ) ;
                //return $reval;
            }
        }
    }

    /*
     *  读取分类缓存信息
     */
    public function sort( $type = 'article', $id = 0, $method = "dataTreeArray" ) {
        if ( !$sort = $this->model->Cache()->get( 'sort' ) ) return false;
        if ( !isset( $sort[ $type ] ) ) return false;
        $tmp = $sort[ $type ];
        unset( $sort );
        if ( $method == null ) return $tmp;
        if ( $method == 'dataTreeArray' ) return $this->Node()->$method( $tmp, $id );
        return $this->Node()->$method( $tmp );
    }

    /*
     * 合并分类与页面
     */
    public function get_sort_page() {
        $sort = array_merge( $this->sort(), $this->get_page() );
        foreach ( $sort as $v ) {
            $tmp[ $v[ 'title' ] ] = $v;
        }
        unset( $sort );
        return $tmp;
    }

    /*
     *  返回文章页面数据
     */
    public function get_page() {
        return $this->model->Cache()->get( 'page' );
    }

    /*
     * isLogin 检查用户是否登录
     */
    public function isLogin() {

        // 检查cookie
        if ( !$cookie = $_COOKIE[ APP_COOKIE ] ) {
            return false;
        }

        $cookie = md6( $cookie, 'decode' );
        $cookie = explode( '|', $cookie );

        list( $uname, $uid, $expire ) = $cookie;

        // 检查cookie是否过期
        if ( !empty( $expire ) && $expire < time() ) {
            return false;
        }

        // 检查用户是否存在
        $this->model->table = 'user';
        if ( !$result = $this->model->find( array( 'id' => $uid, 'author' => $uname ) ) ) {
            return false;
        }

        // 检查用户是否被禁用
        if ( $result[ 'status' ] ) {
            return false;
        }

        unset( $result[ 'pass' ] );
        return $result;
    }

    /*
     * log 日志记录
     *
     * @param $type     日志类别
     * @param $code     日志信息
     */
    public function log( $code, $type = 'system' ) {
        $file = APP_LOG . md6( $type ) . ".php";

        $log_html = "";
        if ( !is_file( $file ) ) {
            $log_html = "<?php die();?>\n";
        }

        $log_ip = S( 'ip' );
        $log_date = time();

        $log_html .= "{$code}=={$log_ip}=={$log_date}\n";
        $f = @fopen( $file, "a+" );
        @fwrite( $f, $log_html );
        @fclose( $f );

        chmod( $file, 0666 );
    }

    /**
     * 返回模板编译后路径
     *
     * @param $file   模板路径及名称
     */
    public function display( $file ) {
        return $this->v->display( $file );
    }

    /**
     * 魔术函数，实现对控制器扩展类的自动加载
     */
    public function __call( $name, $args ) {
        return C( $name )->__input( $this, $args );
    }
}